<?php
namespace app\controller\user;

use app\User;
use app\model\member as member_model;
use app\model\store;
use think\facade\Session;
use think\facade\Cache;

class home extends User
{
    public function userinfo()
    {
        if( $member_id = Session::get( 'member_id' ) )
        {
            //查询用户
            if( $user = member_model::find( $member_id ) )
            {
                //用户信息初始化
                $data = array();
                $data[ 'member_id' ] = $user->member_id;    //用户id

                //查询用户基本信息
                $userinfo = $user->with( [ 'user_data', 'user_auth', 'user_money' ] )->find( 1 )->toArray();

                $data[ 'userinfo' ] = array();
                $data[ 'userinfo' ][ 'phone' ] = $userinfo[ 'phone' ];    //用户手机号
                $data[ 'userinfo' ][ 'name' ] = $userinfo[ 'user_data' ][ 'name' ]; //姓名
                $data[ 'userinfo' ][ 'avatar' ] = $userinfo[ 'user_data' ][ 'avatar' ]; //头像
                $data[ 'userinfo' ][ 'vip' ] = $userinfo[ 'vip' ];    //是否vip用户

                //查询钱包
                $data[ 'money' ] = array();
                $data[ 'money' ][ 'balance' ] = $userinfo[ 'user_money' ][ 'balance' ];
                $data[ 'money' ][ 'reward' ] = $userinfo[ 'user_money' ][ 'reward' ];
                if( $user->store_id )
                {
                    $data[ 'money' ][ 'wage' ] = $userinfo[ 'user_money' ][ 'wage' ];   //如果所属店铺有工资
                }

                //查询权限信息
                if( $userinfo[ 'auth_id' ] != 0 )
                {
                    $permission = array_filter( explode( ',', $userinfo[ 'user_auth' ][ 'permission' ] ) );
                    $menu_list = Cache::get( 'menu' );
// dd( $menu_list );
                    //初始化用户权限列表
                    $data[ 'permission' ] = array();
                    foreach( $menu_list as $key => $menu )
                    {
                        if( in_array( $menu[ 'menu_id' ], $permission ) && ! $menu[ 'hidden' ] )
                        {
                            //vip菜单判断
                            if( ! $userinfo[ 'vip' ] )
                            {
                                if( $menu[ 'vip' ] )
                                {
                                    continue ;
                                }
                            }

                            $data[ 'permission' ][] = $menu[ 'router' ];
                        }
                    }
                }

                //查询店铺信息
                $data[ 'store' ] = array();
                $current_store_id = 0;  //默认店铺
                if( $userinfo[ 'store_id' ] )
                {
                    $current_store_id = $userinfo[ 'store_id' ];
                    $store_list = Store::with( 'store_data' )->select()->toArray();
                }else{
                    $store_list = Store::with( 'store_data', [ 'member_id', $userinfo[ 'member_id' ] ] )->select()->toArray();
                }

                foreach( $store_list as $store )
                {
                    //默认选择店铺
                    if( $store[ 'main' ] )
                    {
                        $current_store_id = $store[ 'store_id' ];
                    }

                    $data[ 'store' ][] = array(
                        'store_id' => $store[ 'store_id' ], //店铺id
                        'main' => $store[ 'main' ], //是否主店铺
                        'name' => $store[ 'name' ], //店铺名称
                        'cert' => $store[ 'cert' ], //是否认证
                        'list_order' => $store[ 'list_order' ], //排序id
                        'phone' => $store[ 'store_data' ][ 'phone' ],   //联系电话
                        'address' => $store[ 'store_data' ][ 'address' ]    //联系地址
                    );
                }

                $data[ 'current_store_id' ] = $current_store_id;
                
                //设置全局店铺id
                Session::set( 'store_id', $current_store_id );

                return $this->response( [ 'status' => 1, 'message' => 'ok', 'data' => $data ] );
            }else{
                return $this->response( [ 'status' => -1, 'message' => '未查询到用户信息' ] );
            }
        }

        return $this->response( [ 'status' => 0, 'message' => '请先登录授权' ] );
    }
}
